<template>
  <div class="follow-box">
    <!-- Header 区域 -->
    <van-nav-bar fixed title="文章详情" left-arrow @click-left="$router.back()" />

    <!-- 文章等待加载  -->
    <!-- 判断detailObj是否为空  就随便拿出detailObj里面的一个属性 判断他是否为undefined，就知道detailObj是否为空了 -->
    <!-- detailObj为空就说明请求还未完成，显示loading -->
    <van-loading color="#1989fa"  vertical v-if="detailObj.title=== undefined">文章加载中...</van-loading>

    <div v-else>
      <!-- 文章信息区域 -->
    <div class="article-container">
      <!-- 文章标题 -->
      <h1 class="art-title">{{detailObj.title }}</h1>

      <!-- 用户信息 -->
      <van-cell center :title="detailObj.aut_name" :label="formatTime(detailObj.pubdate)">
        <template #icon>
          <img :src="detailObj.aut_photo" alt class="avatar" />
        </template>
        <template #default>
          <div >
            <van-button type="info" size="mini" v-if="detailObj.is_followed" @click="Follow(true)">已关注</van-button>
            <van-button icon="plus" type="info" size="mini" plain v-else @click="Follow(false)">关注</van-button>
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider></van-divider>

      <!-- 文章内容 -->
      <div class="art-content" v-html='detailObj.content'></div>

      <!-- 分割线 -->
      <van-divider>End</van-divider>

      <!-- 点赞 -->
       <!-- 点赞(文章) -->
        <!-- attitude:  -1: 无态度，0-不喜欢，1-点赞 数字里面只有0是false -1和1都是true-->
      <div class="like-box">
        <van-button icon="good-job" type="danger" size="small" v-if="detailObj.attitude===1" @click="Praise(1)">已点赞</van-button>
        <van-button icon="good-job-o" type="danger" plain size="small" v-else @click="Praise(0)">点赞</van-button>
      </div>
    </div>
    <!-- 评论列表 -->
    <div>
      <CommentList :target='detailObj.art_id'></CommentList>
    </div>
    </div>
  </div>
</template>

<script>
import CommentList from './CommentList.vue'
import { timeAgo } from '@/utils/date.js'
import { getarticleDetail, likeArticleAPI, unLikeArticleAPI, userFollowedAPI, userUnFollowedAPI } from '@/api'
export default {
  name: 'Detail',
  components: {
    CommentList
  },
  data () {
    return {
      detailObj: {} // 文章详情对象
    }
  },
  created () {
    this.getDetail()
  },
  methods: {
    // 返回xxx天前
    formatTime: timeAgo, // formatTime就是函数  timeAgo是函数体
    // 获取文章详情
    async getDetail () {
      const article_id = this.$route.query.art_id
      const res = await getarticleDetail({ article_id })
      this.detailObj = res.data.data
    },
    // 关注、取消关注
    async Follow (isFollow) {
      if (isFollow === true) { // 已经关注了，这会再点击要取消关注了
        this.detailObj.is_followed = false
        await userUnFollowedAPI({
          target: this.detailObj.aut_id
        })
      } else { // 还没关注，这会再点击要关注了
        this.detailObj.is_followed = true
        await userFollowedAPI({
          target: this.detailObj.aut_id
        })
      }
    },
    // 点赞、取消点赞
    async Praise (attitude) {
      if (attitude === 1) { // 已经点赞了，这会再点击要取消点赞
        this.detailObj.attitude = 0
        await unLikeArticleAPI({
          target: this.detailObj.art_id
        })
      } else { // 还没点赞，这会要点击点赞
        this.detailObj.attitude = 1
        await likeArticleAPI({
          target: this.detailObj.art_id
        })
      }
    }
  }
}
</script>

<style scoped lang="less">
/deep/.van-loading__spinner--circular{
  margin-top: 58px
}
.article-container {
  padding: 10px;
  padding-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
  /deep/ img {
    width: 100%;
  }
  /deep/ pre {
    white-space: pre-wrap;
    word-wrap: break-word;
  }
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
</style>
